using System;
using System.Collections.Generic;
using System.Linq;

// 统一命名空间，避免类找不到
namespace UserDemo
{
    // 1. 抽象类与子类
    public abstract class UserTest
    {
        public abstract void M1();
        public abstract void M2();
        public abstract void M3();
    }

    public class UserTestImpl : UserTest
    {
        public override void M1() => Console.WriteLine("子类实现M1方法");
        public override void M2() => Console.WriteLine("子类实现M2方法");
        public override void M3() => Console.WriteLine("子类实现M3方法");
    }

    // 2. 接口与实现类（结合User实体）
    public interface IUserService
    {
        void Add(User user);
        void Delete(int userId);
        void Update(User user);
        User Query(int userId);
    }

    public class UserServiceImpl : IUserService
    {
        private List<User> _users = new List<User>();

        public void Add(User user)
        {
            _users.Add(user);
            Console.WriteLine($"添加用户成功：{user}");
        }

        public void Delete(int userId)
        {
            var user = _users.FirstOrDefault(u => u.Id == userId);
            if (user != null)
            {
                _users.Remove(user);
                Console.WriteLine($"删除用户成功：ID={userId}");
            }
            else
            {
                Console.WriteLine($"删除失败：未找到ID={userId}的用户");
            }
        }

        public void Update(User user)
        {
            var existingUser = _users.FirstOrDefault(u => u.Id == user.Id);
            if (existingUser != null)
            {
                existingUser.Username = user.Username;
                existingUser.Password = user.Password;
                Console.WriteLine($"更新用户成功：{existingUser}");
            }
            else
            {
                Console.WriteLine($"更新失败：未找到ID={user.Id}的用户");
            }
        }

        public User Query(int userId)
        {
            var user = _users.FirstOrDefault(u => u.Id == userId);
            if (user != null)
                Console.WriteLine($"查询到用户：{user}");
            else
                Console.WriteLine($"未找到ID={userId}的用户");
            return user;
        }
    }

    // 3. User实体类
    public class User
    {
        public int Id { get; set; }
        public string Username { get; set; }
        public string Password { get; set; }

        public User(int id, string username, string password)
        {
            Id = id;
            Username = username;
            Password = password;
        }

        public override string ToString()
        {
            return $"User [Id: {Id}, Username: {Username}]";
        }
    }

    // 4. 练习类（Student、Father等）
    public class Student
    {
        private string name;
        private int age;

        public Student(string name, int age)
        {
            this.name = name;
            this.age = age;
        }

        // 方法名首字母大写（C#规范），避免小写导致的"无法解析"
        public void Study()
        {
            Console.WriteLine($"{name}正在学习");
        }
    }

    public class Father
    {
        protected string familyName;

        public Father(string familyName)
        {
            this.familyName = familyName;
        }

        // 方法名首字母大写
        public void Work()
        {
            Console.WriteLine($"{familyName}父亲在工作");
        }
    }

    public class Child : Father
    {
        private int grade;

        public Child(string familyName, int grade) : base(familyName)
        {
            this.grade = grade;
        }

        public void GoToSchool()
        {
            Console.WriteLine($"{familyName}家的孩子在{grade}年级上学");
        }
    }

    public class Son : Child
    {
        private string hobby;

        public Son(string familyName, int grade, string hobby) : base(familyName, grade)
        {
            this.hobby = hobby;
        }

        public void Play()
        {
            Console.WriteLine($"{familyName}家的儿子喜欢{hobby}");
        }
    }

    // 5. 修正后的Demo类（唯一且包含正确的Main方法）
    public class Demo
    {
        // 主方法必须为首字母大写的Main
        static void Main(string[] args)
        {
            // 测试用户操作
            Console.WriteLine("=== 用户操作测试 ===");
            IUserService userService = new UserServiceImpl();
            userService.Add(new User(1, "zhangsan", "123"));
            userService.Query(1);

            // 测试抽象类
            Console.WriteLine("\n=== 抽象类测试 ===");
            UserTest test = new UserTestImpl();
            test.M1();

            // 测试继承关系
            Console.WriteLine("\n=== 继承关系测试 ===");
            Student student = new Student("张三", 18);
            student.Study(); // 调用大写的Study方法

            Father father = new Father("李");
            father.Work(); // 调用大写的Work方法

            Child child = new Child("王", 3);
            child.Work();
            child.GoToSchool();

            Son son = new Son("张", 5, "踢足球");
            son.Play();
        }
    }
}